@page "/Node"
@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Html.HTable
@using BlazorApp.Pages.Common.Metadata
@using k8s.Models
@using Mapster
@inherits BlazorApp.Pages.Common.TableBase<V1Node>
@if (TableData.PagedItems != null)
{
    <HTable Class="wideTable">
        <HTr>
            <HTd >
                <SearchToolBar Title=@L["Node"] Count="@TableData.Total" OnItemDeletedCallback="@(async () => await InvokeAsync(StateHasChanged))"
                               OnSelectedItemCloseClicked="@TableData.RemoveSelection"
                               OnRemoveAllClicked="@TableData.RemoveAllSelection" TableData="@TableData"
                               OnSearch="OnSearchHandler" TItem="V1Node"
                               OnNsSelected="OnNsSelectedHandler"
                               HideNsSelector="true">
                    @if (_metricsServerReady)
                    {
                        <GridRow Class="grid-table">
                            <GridCol Span="2">
                                <Text Strong>@L["Top"]</Text>
                            </GridCol>
                            <GridCol Span="22">
                                <SimpleSelect Style="width:100px;" Placeholder="@L["Sort By"]"
                                              OnSelectedItemChanged="OnSortByChanged">
                                    <SelectOptions>
                                        <SimpleSelectOption Value="cpu" Label="cpu"></SimpleSelectOption>
                                        <SimpleSelectOption Value="memory" Label="memory"></SimpleSelectOption>
                                    </SelectOptions>
                                </SimpleSelect>
                                <Button Type="@ButtonType.Primary" OnClick="@(() => OnTopClick())">@L["Ok"]</Button>
                            </GridCol>
                        </GridRow>
                    }
                </SearchToolBar>
            </HTd>
        </HTr>
        <HTr>
            <HTd >
                <Table TItem="V1Node"
                       DataSource="@TableData.PagedItems"
                       Size=@TableSize.Small Bordered="true"
                       Total="TableData.Total"
                       @bind-PageIndex="TableData.PageIndex"
                       @bind-PageSize="TableData.PageSize"
                       @bind-SelectedRows="TableData.SelectedRows"
                       Loading="TableData.Loading"
                       OnChange="OnPageChangeHandler">
                    <Selection Key="@(context.Name())"/>
                    <AntDesign.Column Field="@context.Name()" Title=@L["Name"]>
                        <Button OnClick="@(() => OnItemNameClick(context))" Type="@ButtonType.Link">@context.Name()</Button>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Ready"] TData="string">
                        <ConditionsTagView Conditions="@(context.Status.Conditions.Adapt<IList<V1Condition>>())"></ConditionsTagView>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Usage"] TData="string">
                        <NodeStatusBulletChart Node="@context"></NodeStatusBulletChart>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Cordoned"] TData="string" Width="100px">
                        @if (context.Spec.Unschedulable is true)
                        {
                            <Tag Color="@PresetColor.Red.ToString()">Cordoned</Tag>
                        }
                        else
                        {
                            <Tag Color="@PresetColor.Green.ToString()">Normal</Tag>
                        }
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Role"] TData="string">
                        @foreach (var role in GetNodeRole(context))
                        {
                            <ColorfulTag>@role</ColorfulTag>
                        }
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Pods"] TData="string" Width="100px">
                        <ColorfulTag>@PodService.ListByNodeName(context.Name()).Count()</ColorfulTag>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["InternalIP"] TData="string" Width="100px">
                        <ColorfulTag> @context.Status.Addresses.FirstOrDefault(x => x.Type == "InternalIP")?.Address</ColorfulTag>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["ExternalIP"] TData="string" Width="100px">
                        <ColorfulTag> @context.Status.Addresses.FirstOrDefault(x => x.Type == "ExternalIP")?.Address</ColorfulTag>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["KubeletVersion"] TData="string" Width="100px">
                        <ColorfulTag>@context.Status.NodeInfo.KubeletVersion</ColorfulTag>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["ContainerVersion"] TData="string" Width="100px">
                        <ColorfulTag> @context.Status.NodeInfo.ContainerRuntimeVersion</ColorfulTag>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["KernelVersion"] TData="string" Width="100px">
                        <ColorfulTag> @context.Status.NodeInfo.KernelVersion</ColorfulTag>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["OsImage"] TData="string" Width="100px">
                        <ColorfulTag>@context.Status.NodeInfo.OsImage</ColorfulTag>
                    </AntDesign.Column>
                    <AntDesign.Column Title=@L["Age"] TData="string" Width="100px">
                        <AgeView Age="@context.CreationTimestamp()"></AgeView>
                    </AntDesign.Column>
                    <ActionColumn Title=@L["Action"] Width="100px">
                        <NodeAction Item="@context"></NodeAction>
                    </ActionColumn>
                </Table>

            </HTd>
        </HTr>
    </HTable>

    <ResourceWatcher OnResourceChanged="OnResourceChanged" T="V1Node"></ResourceWatcher>
}
